package com.graduationdesign.zengyan.graduationdesignapp.ui.activities.login;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.common.base.Strings;
import com.graduationdesign.zengyan.graduationdesignapp.R;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.User;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.PresenterInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.UiInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseActivity;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseLoadingView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.BaseErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.CommonErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.ErrorConsumer;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.ServerException;
import com.graduationdesign.zengyan.graduationdesignapp.ui.contact.LoginContact;
import com.graduationdesign.zengyan.graduationdesignapp.utils.WidgetUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class LoginActivity extends BaseActivity implements LoginContact.View, BaseLoadingView {
    private LoginContact.Presenter presenter;

    @BindView(R.id.progress_bar)
    ProgressBar progress_bar;

    @BindView(R.id.fab)
    FloatingActionButton fab;

    @BindView(R.id.stunum_edittext)
    EditText mStuNumEditText;

    @BindView(R.id.password_edittext)
    EditText mPasswordEditText;

    private ErrorConsumer mErrorConsumer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        initView();


    }

    @Override
    protected int provideLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected Boolean isFullScreen() {
        return true;
    }

    @Override
    protected void initArgument() {

    }

    @Override
    protected void initPresenter() {
        mErrorConsumer = new ErrorConsumer(mErrorView,"LoginActivity");
        BaseErrorView mErrorView = new CommonErrorView(this);
        presenter = PresenterInjection.provideLoginPresenter(this, mErrorView);

    }

    @Override
    protected void initToolbar() {
    }

    @Override
    protected void initView() {
        fab.setOnClickListener(v -> {
            Observable.just(true)
                    .subscribeOn(Schedulers.io())
                    .map(stringStringMap -> {
                        String stunum = WidgetUtils.getText(mStuNumEditText);
                        String pwd = WidgetUtils.getText(mPasswordEditText);
                        if (Strings.isNullOrEmpty(stunum) || Strings.isNullOrEmpty(pwd)) {
                            throw new ServerException(401, "输入的数据不能为空");
                        }
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("studentNum", stunum);
                        map.put("password", pwd);
                        return map;
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(stringStringMap -> {
                        presenter.login(stringStringMap.get("studentNum"), stringStringMap.get("password"), this);
                    }, mErrorConsumer);
        });
    }

    @Override
    protected void start() {

    }


    @Override
    public void showLoginSuccess(User user) {
        Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
        startActivity(UiInjection.provideNav(this));
        finish();
    }

    @Override
    public void showLoginFail() {
        Toast.makeText(this, "登陆失败", Toast.LENGTH_SHORT).show();
    }



    @Override
    public boolean isLoading() {
        return WidgetUtils.isVisiable(progress_bar);
    }

    @Override
    public void startLoading() {
        progress_bar.setVisibility(View.VISIBLE);
        fab.setAlpha(0f);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.unSubsrible();
    }

    @Override
    public void stopLoading() {
        progress_bar.setVisibility(View.GONE);
        fab.setAlpha(1f);

    }
}
